/**
 * RelEmpreendimento.java
 * Copyright (C) 2009 Jose Mauro da Silva Sandy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package relatorios.classes;

import java.sql.*;
import plagio.postgres.ConnectionPostgres;
import utils.*;

/**
 *
 * @author jmsandy
 */
public class RelEmpreendimento {

    //<editor-fold defaultstate="collapsed" desc="Table Columns">
    static final protected String K_TBL_Empreendimento   = "Empreendimento";
    static final protected String K_TBL_Pessoa           = "Pessoa";
    static final protected String K_COL_cod_objeto       = "cod_objeto";
    static final protected String K_COL_nome             = "nome";
    static final protected String K_COL_status           = "status";
    static final protected String K_COL_dataInicio       = "datainicio";
    static final protected String K_COL_dataTermEfetivo  = "datatermefetivo";
    static final protected String K_COL_dtVenda          = "dtvenda";
    static final protected String K_COL_valorVenda       = "valorvenda";
    static final protected String K_COL_pessoa           = "pessoa";
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public RelEmpreendimento(){
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Methods">
    // Realiza a construcao basica de um select
    private StringBuilder formSelector(){
        StringBuilder query = new StringBuilder();
        query.append("SELECT p." + K_COL_nome + ", e." + K_COL_status + ", e.");
        query.append(K_COL_dataInicio + ", e." + K_COL_dataTermEfetivo +", e.");
        query.append(K_COL_dtVenda + ", e." + K_COL_valorVenda + " FROM ");
        query.append("\"" + K_TBL_Pessoa + "\" p INNER JOIN ");
        query.append("\"" + K_TBL_Empreendimento + "\" e ON ");
        query.append("p." + K_COL_cod_objeto + " = e." + K_COL_pessoa);
        return query;
    }

    /**
     * Recupera informacoes do empreendimento para geracao do relatorio
     * @param dtInicial data inicial de criacao
     * @param dtFinal data final de criacao
     * @param situacao situacao corrente do empreendimento
     * @return um resultset contendo os dados encontrados
     */
    public ResultSet recLstEmpreendimentos(java.util.Date dtInicial,
                                           java.util.Date dtFinal,
                                           Integer situacao){
        ResultSet rs        = null;
        StringBuilder query = this.formSelector();
        StringBuilder crit  = new StringBuilder();

        // Verifica se foi informado uma situacao
        if(situacao != null){
            crit.append(" AND e." + K_COL_status + " = '" + situacao + "'");
        }
        // Verifica se foi informado um intervalo de datas
        if(dtFinal != null){
            crit.append(" AND e. " + K_COL_dataInicio + " BETWEEN '");
            crit.append(dtInicial + "' AND '" + dtFinal + "'");
        }
        try{
            ConnectionPostgres conn = new ConnectionPostgres(); // nova conexao

            // Realiza a conexao com o servidor
            conn = ConnectionSystem.connectionSystem();

            Statement stat = conn.getConnection().createStatement();

            if(crit.toString().startsWith(" AND ")){
                query.append(" WHERE " +
                crit.toString().substring(4, crit.toString().length()));
            }
            rs =  stat.executeQuery(query.toString());
        }catch(Exception ex){
            ex.printStackTrace();
            rs = null;
        }
        return rs;
    }
    //</editor-fold>
}
